package com.yuke.cloud.service.wmc.controller.rpc;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.base.constant.GlobalConstant;
import com.yuke.cloud.common.base.dto.LoginAuthDto;
import com.yuke.cloud.common.base.enums.ErrorCodeEnum;
import com.yuke.cloud.common.base.exception.BusinessException;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.common.util.ThreadLocalMap;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.opc.service.OpcGaodeFeignApi;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoExDto;
import com.yuke.cloud.service.api.smc.dto.SmcShopDto;
import com.yuke.cloud.service.api.wmc.dto.*;
import com.yuke.cloud.service.api.wmc.exceptions.WmcBizException;
import com.yuke.cloud.service.api.wmc.service.WmcStorageFeignApi;
import com.yuke.cloud.service.wmc.controller.frontend.GetPrintOrderController;
import com.yuke.cloud.service.wmc.dto.WmsFlowDto;
import com.yuke.cloud.service.wmc.entity.*;
import com.yuke.cloud.service.wmc.service.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Api(value = "WEB - WmcSpecFeignClient", tags = "处理Wmc相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@Slf4j
@RequestMapping
public class WmcStorageFeignClient implements WmcStorageFeignApi {
    @Resource
    private WmsStorageService wmsStorageService;
    @Resource
    private WmsStorageSpecService wmsStorageSpecService;
    @Resource
    private WmsWarehouseShopService wmsWarehouseShopService;
    @Resource
    private AssGoodsReturnService assGoodsReturnService;
    @Autowired
    private GetPrintOrderController getPrintOrderController;
    @Autowired
    private WmsWarehouseService wmsWarehouseService;
    @Autowired
    private WmsWarehouseGroupService wmsWarehouseGroupService;
    @Autowired
    private WmsGiftService wmsGiftService;
    @Autowired
    private DicRegionService dicRegionService;
    @Resource
    private OpcGaodeFeignApi opcGaodeFeignApi;
    @Resource
    private WmsFlowService wmsFlowService;



    /**
     * 店铺与仓库之间的最大关联距离，单位米
     */
    @Value("${yuke.warehouse.max-distance}")
    private Long maxDistance;

    @Autowired
    private WmsStockInService wmsStockInService;


    protected LoginAuthDto getLoginAuthDto() {
        // todo 20181101 mod by wg 修改成模拟数据，暂时测试用，生产环境后需要还原
        LoginAuthDto loginAuthDto = (LoginAuthDto) ThreadLocalMap.get(GlobalConstant.Sys.TOKEN_AUTH_DTO);

        if (PublicUtil.isEmpty(loginAuthDto)) {
            throw new BusinessException(ErrorCodeEnum.UAC10011041);
        }
        return loginAuthDto;
    }

    @Override
    public List<StorageDto> getStorageNum(@RequestBody List<StorageDto> dto) {
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < dto.size(); i++) {
            list.add(dto.get(i).getShopId());
        }

        if (list.size() == 0) {
            return null;
        }
        List<WmsWarehouseShop> getbyshopid = wmsWarehouseShopService.getbyshopid(list);
        if (getbyshopid.size() == 0) {
            return null;
        }
        List<StorageDto> dto1 = new ArrayList<>();
        for (int i = 0; i < dto.size(); i++) {
            for (int j = 0; j < getbyshopid.size(); j++) {
                if (dto.get(i).getShopId().toString().equals(getbyshopid.get(j).getShopId().toString())) {
                    StorageDto storageDto = new StorageDto();
                    storageDto.setShopId(dto.get(i).getShopId());
                    storageDto.setSpecId(dto.get(i).getSpecId());
                    storageDto.setWarehouseId(getbyshopid.get(j).getWarehouseId());
                    dto1.add(storageDto);
                }
            }
        }

        List<WmsStorage> list1 = wmsStorageService.getStorageNum(dto1);
        for (int j = 0; j < dto1.size(); j++) {
            boolean flag = true;
            for (int i = 0; i < list1.size(); i++) {
                if (list1.get(i).getWarehouseId().toString().equals(dto1.get(j).getWarehouseId().toString()) && list1.get(i).getSpecId().toString().equals(dto1.get(j).getSpecId().toString())) {
                    flag = false;
                    WmsStorage wmsStorage = list1.get(i);
                    dto1.get(j).setNumber(wmsStorage.getNumber() - wmsStorage.getLockNumber());
                }
            }
            if (flag) {
                dto1.get(j).setNumber(0);
            }
        }
        return dto1;
    }

    @Override
    public List<Long> getshopidbywarehousename(Long warehouseId) {
        WmsWarehouseShop wmsWarehouseShop = new WmsWarehouseShop();
        wmsWarehouseShop.setWarehouseId(warehouseId);
        List<WmsWarehouseShop> select = wmsWarehouseShopService.select(wmsWarehouseShop);
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < select.size(); i++) {
            list.add(select.get(i).getShopId());
        }
        return list;
    }

    @Override
    public List<Map> getWarehouseName(@RequestBody List<Long> shopids) {
        List<Map> list = wmsWarehouseShopService.getbyforeachsql(shopids);
        return list;
    }

    @Override
    public Wrapper<PageInfo<Map>> getassreturn(@RequestBody Map map) {

        Example example = new Example(AssGoodsReturn.class);
        example.setOrderByClause("create_time desc"); // add by wg 20190404
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderType", 1);
        criteria.andEqualTo("returnStatus", 5);
        criteria.andEqualTo("shopId", map.get("shopId"));

        // add by wg 20190403 增加分页查询
        Integer page = 0;
        Integer size = 0;
        if (map.get("page") != null) {
            page = Integer.valueOf(map.get("page").toString());
        }
        if (map.get("size") != null) {
            page = Integer.valueOf(map.get("size").toString());
        }

        if (page < 0) {
            page = 0;
        }
        if (size < 0) {
            size = 0;
        }

        PageHelper.startPage(page, size);


        List<AssGoodsReturn> assGoodsReturns = assGoodsReturnService.selectByExample(example);
        if (assGoodsReturns.size() == 0) {
            List<Map> list = new ArrayList<>();
            PageInfo pageInfo = new PageInfo(list);
            return WrapMapper.ok(pageInfo);
//            return list;
        }
        List<Map> list = new ArrayList<>();
        Map map1 = null;
        for (AssGoodsReturn assGoodsReturn : assGoodsReturns) {
            map1 = new HashMap();
            map1.put("orderId", assGoodsReturn.getOrderId());
            map1.put("goodsReturnId", assGoodsReturn.getGoodsReturnId());
            list.add(map1);
        }
        System.err.println(list);

        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
//        return list;
    }

    @Override
    public Wrapper<PageInfo<Map>> getassreturn2(@RequestBody Map map) {
        Example example = new Example(AssGoodsReturn.class);
        example.setOrderByClause("create_time desc");
        Example.Criteria criteria = example.createCriteria();
        if (map.get("returnStatus") != null && !map.get("returnStatus").equals("")) {
            if (map.get("returnStatus").toString().equals("67")) {

                List list = new ArrayList<>();
                list.add(7);
                list.add(6);
                criteria.andIn("returnStatus", list);
            } else {
                criteria.andEqualTo("returnStatus", map.get("returnStatus"));
            }
        }
        if (map.get("orderType") != null && !map.get("orderType").equals("")) {
            criteria.andEqualTo("orderType", map.get("orderType"));
        }
        if (map.get("shopId") != null && !map.get("shopId").equals("")) {
            criteria.andEqualTo("shopId", map.get("shopId"));
        }
        if (map.get("userId") != null && !map.get("userId").equals("")) {
            criteria.andEqualTo("userId", map.get("userId"));
        }

        // add by wg 20190403 增加分页查询
        Integer page = 0;
        Integer size = 0;
        if (map.get("page") != null) {
            page = Integer.valueOf(map.get("page").toString());
        }
        if (map.get("size") != null) {
            page = Integer.valueOf(map.get("size").toString());
        }

        if (page < 0) {
            page = 0;
        }
        if (size < 0) {
            size = 0;
        }

        PageHelper.startPage(page, size);

        List<AssGoodsReturn> assGoodsReturns = assGoodsReturnService.selectByExample(example);
        List<Map> list = new ArrayList<>();
        Map map1;
        for (AssGoodsReturn assGoodsReturn : assGoodsReturns
                ) {
            map1 = new HashMap();
            map1.put("orderId", assGoodsReturn.getOrderId());
            map1.put("returnStatus", assGoodsReturn.getReturnStatus());
            map1.put("is_refund", assGoodsReturn.getIsRefund());
            list.add(map1);
        }
//        return list;
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);


    }

    @Override
    public Wrapper<PageInfo<Map>> getassreturn3(@RequestBody Map map) {
        Example example = new Example(AssGoodsReturn.class);
        example.setOrderByClause("create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderType", map.get("orderType"));

        if (map.get("shopId") != null && !map.get("shopId").equals("")) {
            criteria.andEqualTo("shopId", map.get("shopId"));
        }

        // add by wg 20190403 增加分页查询
        Integer page = 0;
        Integer size = 0;
        if (map.get("page") != null) {
            page = Integer.valueOf(map.get("page").toString());
        }
        if (map.get("size") != null) {
            page = Integer.valueOf(map.get("size").toString());
        }

        if (page < 0) {
            page = 0;
        }
        if (size < 0) {
            size = 0;
        }

        PageHelper.startPage(page, size);

        List<AssGoodsReturn> assGoodsReturns = assGoodsReturnService.selectByExample(example);
        List<Map> list = new ArrayList<>();
        Map map1;
        for (AssGoodsReturn assGoodsReturn : assGoodsReturns) {
            map1 = new HashMap();
            map1.put("orderId", assGoodsReturn.getOrderId());
            map1.put("returnStatus", assGoodsReturn.getReturnStatus());
            map1.put("is_refund", assGoodsReturn.getIsRefund());
            list.add(map1);
        }

        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
//        return list;


    }

    @Override
    public Object getlogistics(@RequestBody Map map) throws Exception {
        Wrapper logistics = getPrintOrderController.getLogistics(map);
        Object result = logistics.getResult();
        return result;
    }

    @Override
    public Long getwarehouseinfo(Long shopid) {
        WmsWarehouseShop wmsWarehouseShop = new WmsWarehouseShop();
        wmsWarehouseShop.setShopId(shopid);
        List<WmsWarehouseShop> select = wmsWarehouseShopService.select(wmsWarehouseShop);
        Long warehouseId = select.get(0).getWarehouseId();
        Long regionCityId = wmsWarehouseService.findById(warehouseId).getRegionCityId();
        return regionCityId;
    }

    // 处理仓库商品锁定库存变化与preadd2一样
    @Override
    public Wrapper preadd(@RequestParam("name") String name) {
        JSONArray jsonArray = JSONArray.parseArray(name);
        System.err.println("进入wmc系统");
        log.info("========jsonArray={}", name);  // add for debug 20190322
        for (int i = 0; i < jsonArray.size(); i++) {
            Map jsonObject = jsonArray.getJSONObject(i);
            Object shopId = jsonObject.get("shopId");
            Object specId = jsonObject.get("specId");
            Object goodsNum = jsonObject.get("goodsNum");
            // add by wg 20190511
            Object isFactory = jsonObject.get("isFactory");
            if (PublicUtil.isNotEmpty(isFactory) && Boolean.valueOf(isFactory.toString())) {
                Long warehouseId = 0L;
                wmsWarehouseService.bookgoods(warehouseId, Long.valueOf(specId.toString()), Integer.valueOf(goodsNum.toString()));
            } else {

                // mod by wg 20190727
                Long warehouseId = null;
                if (jsonObject.get("warehouseId") != null) {
                    warehouseId = Long.valueOf(jsonObject.get("warehouseId").toString());
                }
                if (warehouseId == null || warehouseId <= 0L) {
                    //根据商品id查找店铺id
                    Long aLong = Long.valueOf(shopId.toString());
                    WmsWarehouseShop wmsWarehouseShop = new WmsWarehouseShop();
                    wmsWarehouseShop.setShopId(aLong);
                    List<WmsWarehouseShop> select = wmsWarehouseShopService.select(wmsWarehouseShop);
                    warehouseId = select.get(0).getWarehouseId();
                }
                System.out.println("锁定库存goodsNum"+goodsNum);
                wmsWarehouseService.bookgoods(warehouseId, Long.valueOf(specId.toString()), Integer.valueOf(goodsNum.toString()));
            }

            System.out.println("仓库预定商品成功");
        }
        return WrapMapper.ok("ok");
    }

    @Override
    public Wrapper getwarehouseidbyshopid(@RequestParam("shopid") Long shopid) {
        if(shopid == null){
            System.out.println("getwarehouseidbyshopid（ShopId）传入ShopId为空,默认值返回值为6武汉白沙洲仓");
            return  WrapMapper.ok(6L);
        }
        WmsWarehouseShop wmsWarehouseShop = new WmsWarehouseShop();
        wmsWarehouseShop.setShopId(shopid);
        List<WmsWarehouseShop> select = wmsWarehouseShopService.select(wmsWarehouseShop);
        if(select!=null && select.size()>0){
            WmsWarehouseShop wmsWarehouseShop1 = select.get(0);
            return WrapMapper.ok(wmsWarehouseShop1.getWarehouseId());
        }

       return  WrapMapper.ok(6L);
    }

    @Override
    public boolean ifexits(@RequestParam("orderId") Long orderId) {
        AssGoodsReturn byId = assGoodsReturnService.findById(orderId);
        if (byId == null) {
            return false;
        } else {
            return true;
        }

    }

    // 处理仓库商品锁定库存变化
    @Override
    public Wrapper preadd2(@RequestParam("name") String name) {
        JSONArray jsonArray = JSONArray.parseArray(name);
        for (int i = 0; i < jsonArray.size(); i++) {
            Map jsonObject = jsonArray.getJSONObject(i);
            Object shopId = jsonObject.get("shopId");
            Object specId = jsonObject.get("specId");
            Object goodsNum = jsonObject.get("goodsNum");
            //根据商品id查找店铺id
            Long aLong = Long.valueOf(shopId.toString());
            WmsWarehouseShop wmsWarehouseShop = new WmsWarehouseShop();
            wmsWarehouseShop.setShopId(aLong);
            List<WmsWarehouseShop> select = wmsWarehouseShopService.select(wmsWarehouseShop);
            Long warehouseId = select.get(0).getWarehouseId();
            WmsStorage wmsStorage = new WmsStorage();
            wmsStorage.setSpecId(Long.valueOf(specId.toString()));
            wmsStorage.setWarehouseId(warehouseId);
            List<WmsStorage> select1 = wmsStorageService.select(wmsStorage);
            WmsStorage wmsStorage1 = select1.get(0);
            wmsStorage.setStorageId(wmsStorage1.getStorageId());
            //wmsStorage.setNumber(wmsStorage1.getNumber() + Integer.valueOf(goodsNum.toString()));
            wmsStorage.setLockNumber(wmsStorage1.getLockNumber() + Integer.valueOf(goodsNum.toString()));
            wmsStorage.setUpdateTime(new Date());

            log.info("++++++++getLockNumber={}, goodsNum={}", wmsStorage1.getLockNumber(), goodsNum); // add for debug 20190322

            wmsStorageService.update(wmsStorage);
        }
        return WrapMapper.ok();
    }

    @Override
    public Integer getrefundamound(@RequestParam("orderId") Long orderId) {
        AssGoodsReturn assGoodsReturn = new AssGoodsReturn();
        assGoodsReturn.setOrderId(orderId);
        // add by wg 20190327 增加限制条件
        assGoodsReturn.setIsRefund(true);
        assGoodsReturn.setOrderType((byte)1);
        List<AssGoodsReturn> select = assGoodsReturnService.select(assGoodsReturn);
        int money = 0;
        for (AssGoodsReturn assGoodsReturn1 : select) {
//            if (assGoodsReturn1.getRefundPaid() != null && assGoodsReturn1.getRefundPaid().equals("")) {
            if (assGoodsReturn1.getRefundPaid() != null) {
                money += assGoodsReturn1.getRefundPaid();
            }

        }
        return money;
    }

    // 处理仓库商品实际库存变化
    @Override
    public void preadd3(@RequestParam("name") String sz) {
        JSONArray jsonArray = JSONArray.parseArray(sz);
        for (int i = 0; i < jsonArray.size(); i++) {
            Map jsonObject = jsonArray.getJSONObject(i);
            Object shopId = jsonObject.get("shopId");
            Object specId = jsonObject.get("specId");
            Object goodsNum = jsonObject.get("goodsNum");
            //根据商品id查找店铺id
            Long aLong = Long.valueOf(shopId.toString());
            WmsWarehouseShop wmsWarehouseShop = new WmsWarehouseShop();
            wmsWarehouseShop.setShopId(aLong);
            List<WmsWarehouseShop> select = wmsWarehouseShopService.select(wmsWarehouseShop);
            Long warehouseId = select.get(0).getWarehouseId();
            WmsStorage wmsStorage = new WmsStorage();
            wmsStorage.setSpecId(Long.valueOf(specId.toString()));
            wmsStorage.setWarehouseId(warehouseId);
            List<WmsStorage> select1 = wmsStorageService.select(wmsStorage);
            WmsStorage wmsStorage1 = select1.get(0);
            wmsStorage.setStorageId(wmsStorage1.getStorageId());
            wmsStorage.setNumber(wmsStorage1.getNumber() + Integer.valueOf(goodsNum.toString()));
            wmsStorageService.update(wmsStorage);
        }

    }

    @Override
    public int assessinfo(@RequestParam("orderId") Long orderId) {

        if(orderId==null||orderId.toString().equals("")){
            return 0;
        }
        AssGoodsReturn assGoodsReturn = new AssGoodsReturn();
        assGoodsReturn.setOrderId(orderId);

        List<AssGoodsReturn> returns = assGoodsReturnService.select(assGoodsReturn);
        if(returns.size()==0){
            return 0;
        }

        int money=0;
        for(int i=0;i<returns.size();i++){
            Integer refundPaid = returns.get(i).getRefundPaid();
            money+=refundPaid;
        }
        return money;
    }

    /**
     * 获取同一个城市仓库中指定商品的库存量
     * @param regionCityId
     * @return
     */
    @Override
    public Wrapper<Map<Long, Integer>> getCityStockCount(@RequestParam("regionCityId") Long regionCityId, @RequestBody List<Long> specIds) {

        Map<Long, Integer> stockCountMap = wmsStorageService.getCityStockCount(specIds, regionCityId);
        return WrapMapper.ok(stockCountMap);
    }

    /**
     * 获取同一个城市可卖商品数量
     * @param regionCityId
     * @return
     */
    @Override
    public Wrapper<Map<Long, Integer>> getCityAllStockCount(@RequestParam("regionCityId") Long regionCityId) {

        Map<Long, Integer> stockCountMap = wmsStorageService.getCityAllStockCount(regionCityId);
        return WrapMapper.ok(stockCountMap);
    }

    /**
     * 获取远程仓库可卖商品数量及仓库信息
     * @param localWarehouseId
     * @return
     */
    @Override
    public Wrapper<List<WareProductDto>> getRemoteStock(@RequestBody List<Long> specIds, @RequestParam("localWarehouseId") Long localWarehouseId) {
        List<WareProductDto> list = wmsStorageService.getRemoteStockCount(specIds, localWarehouseId);

        return WrapMapper.ok(list);
    }

    /**
     * 获取指定仓库中指定商品的库存量
     * @param warehouseId
     * @return
     */
    @Override
    public Wrapper<Map<Long, Integer>> getWarehouseStockCount(@RequestParam("warehouseId") Long warehouseId, @RequestBody List<Long> specIds) {
        Map<Long, Integer> stockCountMap = wmsStorageService.getWarehouseStockCount(warehouseId, specIds);
        return WrapMapper.ok(stockCountMap);
    }

    /**
     * 根据仓库ID获取仓库名称等信息
     * @param warehouseIds
     * @return
     */
    @Override
    public Wrapper<List<WarehouseInfoDto>> getWarehouseInfo(@RequestBody List<Long> warehouseIds) {
        List<WarehouseInfoDto> list = wmsWarehouseService.getWarehouseInfo(warehouseIds);

        return WrapMapper.ok(list);
    }

    @Override
    public List<StorageDto> getStorageInfo() {
       List<WmsStorage> storageList = wmsStorageService.getStorageInfo();
        List<StorageDto> storageDtos = new ArrayList<>();
        if (storageList!=null && storageList.size()>0) {
            for (WmsStorage item : storageList) {
                if (item.getNumber() < 25) {
                    StorageDto storage = new StorageDto();
                    storage.setNumber(item.getNumber() + 200);
                    storage.setSpecId(item.getSpecId());
                    storage.setWarehouseId(item.getWarehouseId());
                    storageDtos.add(storage);
                }

            }
        }
        return storageDtos;
    }

    @Override
    public Wrapper updateStorageNum(@RequestBody List<StorageDto> storageDtos) {
        Long userId = getLoginAuthDto().getUserId();//RequestUtil.getLoginUser().getUserId()

        for (StorageDto item : storageDtos){
            item.setUpdateId(userId);
        }

        wmsStorageService.updateByStorageDtos(storageDtos);
        return WrapMapper.ok();
    }

    @Override
    public List<WarehouseInfoDto> getWarehouseInfoByGroupId(@RequestParam("groupId") Long groupId) {
        List<WarehouseInfoDto> warehouseInfoDto = wmsWarehouseGroupService.getWarehouseInfoByGroupId(groupId);
        return warehouseInfoDto;
    }

    @Override
    public List<StorageSpecInfoDto> getStorageInfoByCategoryIds(@RequestBody List<Long> categoryIds,@RequestParam("shopId") Long shopId) {
        Wrapper<Long> wrapper = getwarehouseidbyshopid(shopId);
        Long warehouseId = wrapper.getResult();
        List<StorageSpecInfoDto> storageSpecInfoDtos = wmsStorageSpecService.getStorageInfoByCategoryIds(categoryIds,warehouseId);
        return storageSpecInfoDtos;
    }

    /**
     * 各种排序和过滤 is_enable = 1 salePrice
     * @param storageSpecInfoDto
     * @param shopId
     * @return
     */
    @Override
    public List<StorageSpecInfoDto> getStorageInfoByCondition(@RequestBody StorageSpecInfoDto storageSpecInfoDto,@RequestParam("shopId") Long shopId) {
        Wrapper<Long> wrapper = getwarehouseidbyshopid(shopId);
        Long warehouseId = wrapper.getResult();
        storageSpecInfoDto.setWarehouseId(warehouseId);

        List<StorageSpecInfoDto> spec = wmsStorageSpecService.getAppStorageSpecByCondition(storageSpecInfoDto,shopId);
        if (PublicUtil.isEmpty(spec) ){
            return spec;
        }
        spec = wmsStorageSpecService.getSpecInfoExtra(shopId,spec,storageSpecInfoDto.getSaleType(),storageSpecInfoDto.getWarehouseId());

        return spec;
    }

    /**
     * is_enable = 1  pmcFeignApi.getSpecDtosBySpecIds
     * @param specIds
     * @param shopId
     * @return
     */
    @Override
    public List<StorageSpecInfoDto> getStorageInfoBySpecIds(@RequestBody List<Long> specIds,@RequestParam("shopId") Long shopId) {
        Wrapper<Long> wrapper = getwarehouseidbyshopid(shopId);
        Long warehouseId = wrapper.getResult();
        List<StorageSpecInfoDto> storageSpecInfoDtos = wmsStorageSpecService.getStorageInfoBySpecIds(specIds,warehouseId);
        return storageSpecInfoDtos;
    }

    @Override
    public StorageDto getStorageInfos(Long specId) {
        System.out.println("================================"+specId);
        StorageDto storageSpecInfoDto = new StorageDto();
        //WmsStorage wmsStorage = wmsStorageService.findById(specId);
        Example example = new Example(WmsStorage.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("specId",specId);
        List<WmsStorage> wmsStorages = wmsStorageService.selectByExample(example);
        if (wmsStorages!=null&&wmsStorages.size()>0){
            System.out.println("++++++++++++++++++++"+wmsStorages);
            for (WmsStorage wmsStorage:wmsStorages )
            {
                BeanUtils.copyProperties(wmsStorage,storageSpecInfoDto);
                Example example1 = new Example(WmsWarehouse.class);
                Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("warehouseId",wmsStorage.getWarehouseId());
                List<WmsWarehouse> wmsWarehouses = wmsWarehouseService.selectByExample(example1);
                if (wmsWarehouses!=null&& wmsWarehouses.size()>0){
                    for (WmsWarehouse wmsWarehouse : wmsWarehouses) {
                        storageSpecInfoDto.setRegionCityId(wmsWarehouse.getRegionCityId());
                    }
                }
                System.out.println("\\\\\\\\\\\\\\\\\\");
            }
        }
        return storageSpecInfoDto;
    }

    /**先获取商品信息，在从库存信息里面拿价格，仓库信息等值
     * 积分商品   is_enable = 1
     * @param shopId
     * @param specIds
     * @param cartType
     * @return
     */
    @Override
    public List<ProdSpecInfoExDto> getStorageSpecBySpecIds(@RequestParam("shopId") Long shopId, @RequestBody List<Long> specIds, @RequestParam("cartType") Byte cartType){
        Wrapper wrapper = getwarehouseidbyshopid(shopId);

        Long warehouseId = Long.parseLong(wrapper.getResult().toString());

        return wmsStorageSpecService.getStorageSpecBySpecIds(warehouseId,specIds,cartType);
    }

    /**先获取商品信息，在从库存信息里面拿价格，仓库信息等值
    *适用于秒杀商品，活动商品的商品信息获取
     * 调用的 getStorageInfoBySpecIds    is_enable = 1
     */
    @Override
    public List<StorageSpecInfoDto> getProdStorageSpecBySpecIds(@RequestBody List<Long> specIds, @RequestParam("shopId") Long shopId){
        Wrapper wrapper = getwarehouseidbyshopid(shopId);
        Long warehouseId = Long.parseLong(wrapper.getResult().toString());
        return wmsStorageSpecService.getProdStorageSpecBySpecIds(specIds, warehouseId);

    }



    @Override
    public int bindWarehouseShop(@RequestBody SmcShopDto smcShopDto){
        System.out.println(smcShopDto.getLatitude()+" ,"+smcShopDto.getLongitude());
        List<Map> list = wmsWarehouseService.findByPostion(smcShopDto.getLatitude(), smcShopDto.getLongitude(), maxDistance);
        System.out.println(list);
        log.info("leave findByPosition={}",list);

        if (list.size()>0){
            Long warehouseId = Long.parseLong(list.get(0).get("warehouseId").toString());
            WmsWarehouseShop wmsWarehouseShop = new WmsWarehouseShop();
            wmsWarehouseShop.setShopId(smcShopDto.getShopId());
            wmsWarehouseShop.setWarehouseId(warehouseId);

            wmsWarehouseShopService.save(wmsWarehouseShop);
                }
        return 0;
    }

    @Override
    public Wrapper<List<WareProductDto>> getInfoWarehouseIds(@RequestBody List<Long> specIds) {
        List<WareProductDto> infoWarehouseIds = wmsStorageService.getInfoWarehouseIds(specIds);
        return WrapMapper.ok(infoWarehouseIds);
    }

    @Override
    public List<StorageSpecInfoDto> getProdSpecInfo(@RequestBody List<Long> specIds) {
        List<StorageSpecInfoDto> prodSpecInfo = wmsStorageSpecService.getProdSpecInfo(specIds);
        return prodSpecInfo;
    }

    @Override
    public Wrapper getGroupIdByShopId(@RequestParam("shopId") Long shopId) {
        if(shopId == null){
            System.out.println("getGroupIdByShopId（ShopId）传入ShopId为空,默认值返回值为6武汉白沙洲仓");
            return  WrapMapper.ok(220839532667469824L);
        }
        System.out.println("传入ShopId为空,默认值返回值为6武汉白沙洲仓"+shopId);
        Long groupId = wmsWarehouseGroupService.findGroupId(shopId);
        if(groupId!=null && groupId>0){
            return  WrapMapper.ok(groupId);
        }

        return  WrapMapper.ok(220839532667469824L);
    }

    /**
     * storageIds
     * @param storageIds
     * @param cartType
     * @return
     */
    @Override
    public List<ProdSpecInfoExDto> getStorageSpecByStorageIds(@RequestBody List<Long> storageIds, @RequestParam("cartType") Byte cartType){
        return wmsStorageSpecService.getStorageSpecByStorageIds(storageIds,cartType);
    }

    @Override
    public List<StorageSpecInfoDto> getWmsSpecInfoBySpecIds(@RequestBody List<Long> specIds,@RequestParam("shopId")Long shopId ){
        Wrapper wrapper = getwarehouseidbyshopid(shopId);
        Long warehouseId = Long.parseLong(wrapper.getResult().toString());
        return wmsStorageService.getWmsSpecInfoBySpecIds(warehouseId,specIds);

    }

    @Override
    public List<WmsGiftDto> getGiftList(@RequestBody WmsGiftDto wmsGiftDto){
        if (wmsGiftDto.getWarehouseId() == null && wmsGiftDto.getShopId() != null){
            Wrapper wrapper = getwarehouseidbyshopid(wmsGiftDto.getShopId());
            Long warehouseId = Long.parseLong(wrapper.getResult().toString());
            wmsGiftDto.setWarehouseId(warehouseId);
        }

        return wmsGiftService.getGiftList(wmsGiftDto);
    }

    @Override
    public Integer updateWmsGift(@RequestBody WmsGiftDto wmsGiftDto){
        return wmsGiftService.updateWmsGift(wmsGiftDto);
    }

    /**
     * 不对isEnable限制,有pmcSpecInfo
     * @param specIds
     * @param shopId
     * @return
     */
    @Override
    public List<StorageSpecInfoDto> getAllStorageInfoBySpecIds(@RequestBody List<Long> specIds,@RequestParam("shopId") Long shopId) {
        Wrapper<Long> wrapper = getwarehouseidbyshopid(shopId);
        Long warehouseId = Long.parseLong(wrapper.getResult().toString());
        List<StorageSpecInfoDto> storageSpecInfoDtos = wmsStorageSpecService.getAllStorageInfoBySpecIds(specIds,warehouseId);
        return storageSpecInfoDtos;
    }

    /**
     * 不对isEnable限制，无pmcSpecInfo
     * @param specIds
     * @param warehouseId
     * @return
     */
    @Override
    public List<StorageSpecInfoDto> getAllStorageBySpecIds(@RequestBody List<Long> specIds,@RequestParam("warehouseId") Long warehouseId) {
        List<StorageSpecInfoDto> storageSpecInfoDtos = wmsStorageSpecService.getAllStorageBySpecIds(specIds,warehouseId);
        return storageSpecInfoDtos;
    }
    @Override
    public List<Map> findByPostion(Double latitude, Double longitude,Long distance){
        List<Map> list = wmsWarehouseService.findByPostion(latitude, longitude, distance);
        return list;
    }

    @Override
    public List<Long>  getBrandsByCategorys(@RequestBody List<Long> prodCategories,@RequestParam("specName") String specName,@RequestParam("saleType") Byte saleType,@RequestParam("shopId") Long shopId) {
        Wrapper<Long> wrapper = getwarehouseidbyshopid(shopId);
        Long warehouseId = wrapper.getResult();
        List<StorageSpecInfoDto> storageSpecInfoDtos = wmsStorageSpecService.getBrandsByCategorys(prodCategories,specName,saleType,warehouseId);
        List<Long> brands = storageSpecInfoDtos.stream().distinct().map(StorageSpecInfoDto::getBrandId).collect(Collectors.toList());

        return brands;
    }


    @Override
    public List<StorageDto> getStorageInfoByStorageIds(@RequestBody List<Long> storageIds) {
        List<StorageDto> storageDtos = wmsStorageService.getStorageInfoByStorageId(storageIds);
        return storageDtos;
    }

    @Override
    public Wrapper<List<WarehouseInfoDto>> getAllWarehouse(@RequestParam(name="warehouseName",required = false) String warehouseName,
                           @RequestParam(name="wareType",required = false) String wareType) {

        Example example = new Example(WmsWarehouse.class);
        Example.Criteria criteria = example.createCriteria();
        if (warehouseName != null) {
            criteria.andLike("warehouseName", '%' + warehouseName + '%');
        }
        if (wareType != null) {
            criteria.andEqualTo("wareType", wareType);
        }
        List<WmsWarehouse> wmsWarehouses = wmsWarehouseService.selectByExample(example);
        List<WarehouseInfoDto> wmsWarehouseList = new ArrayList<>();
        for (WmsWarehouse wmsWarehouse : wmsWarehouses){
            WarehouseInfoDto warehouseInfoDto = new WarehouseInfoDto();
            BeanUtils.copyProperties(wmsWarehouse,warehouseInfoDto);
            wmsWarehouseList.add(warehouseInfoDto);
        }
        return WrapMapper.ok(wmsWarehouseList);
    }

   @Override
    public Wrapper<List<StorageDto>> selectWmsSpecsByProductId(@RequestBody StorageDto storageDto) {
        if (storageDto == null || storageDto.getWarehouseId() == null || storageDto.getProductId() == null){
            return  WrapMapper.error("参数传递错误！");
        }

        List<StorageDto> wmsStorageList = wmsStorageSpecService.selectWmsSpecsByProductId(storageDto);
        return WrapMapper.ok(wmsStorageList);
    }


    @Override
    public WarehouseInfoDto saveWarehouse(@RequestBody WarehouseInfoDto warehouseInfoDto){
        WmsWarehouse wmsWarehouse = new WmsWarehouse();
        BeanUtils.copyProperties(warehouseInfoDto,wmsWarehouse);

        Example example = new Example(DicRegion.class);
        Example.Criteria criteria = example.createCriteria();
        List list = new ArrayList();
        list.add(wmsWarehouse.getRegionProvinceId());
        list.add(wmsWarehouse.getRegionCityId());
        list.add(wmsWarehouse.getRegionCountyId());
        criteria.andIn("regionId", list);
        List<DicRegion> dicRegions = dicRegionService.selectByExample(example);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < dicRegions.size(); i++) {
            sb.append(dicRegions.get(i).getRegionName());
        }
        sb.append(wmsWarehouse.getAddress());
        System.out.println("这个这个" + opcGaodeFeignApi.getLonLarByAddr(sb.toString(), dicRegions.get(1).getRegionName()));
        Wrapper<Map> lonLarByAddr = opcGaodeFeignApi.getLonLarByAddr(sb.toString(), dicRegions.get(1).getRegionName());
        System.out.println("opcGaodeFeignApi.getLonLarByAddr ->" + lonLarByAddr);
        if (lonLarByAddr.error()) {
            throw new WmcBizException(ErrorCodeEnum.GL99990002);
        }
        if (lonLarByAddr.getResult() == null) {
            log.info("param:addr={}, city={}, lonLarByAddr={}", sb.toString(), dicRegions.get(1).getRegionName(), lonLarByAddr);
            return null;
        }
        Iterator<Map.Entry> iter = lonLarByAddr.getResult().entrySet().iterator();
        String addrInfo = "";
        while (iter.hasNext()) {
            addrInfo = iter.next().getValue().toString();
        }
        if (addrInfo == "") {
            log.info("param:addr={}, city={}, lonLarByAddr={}", sb.toString(), dicRegions.get(1).getRegionName(), lonLarByAddr);
            return null;
        }
        List<Double> location = Arrays.asList(addrInfo.split(",")).stream().map(l -> Double.parseDouble(l)).collect(Collectors.toList());
        wmsWarehouse.setLongitude(new BigDecimal(location.get(0)));
        wmsWarehouse.setLatitude(new BigDecimal(location.get(1)));
        wmsWarehouse.setRunStatus((byte)1);
        wmsWarehouse.setWareType((byte)2);
        wmsWarehouse.setWareCategory((byte)2);
        wmsWarehouseService.save(wmsWarehouse);
        warehouseInfoDto.setWarehouseId(wmsWarehouse.getWarehouseId());
        warehouseInfoDto.setLatitude(wmsWarehouse.getLatitude().doubleValue());
        warehouseInfoDto.setLongitude(wmsWarehouse.getLongitude().doubleValue());
        return warehouseInfoDto;
    }

    @Override
    public Wrapper saveWarehouseGroup(Long groupId, Long warehouseId){
        WmsWarehouseGroup warehouseGroup = new WmsWarehouseGroup();
        warehouseGroup.setGroupId(groupId);
        warehouseGroup.setWarehouseId(warehouseId);
        wmsWarehouseGroupService.save(warehouseGroup);
        return WrapMapper.ok();
    }

    @Override
    public Wrapper saveWarehouseShop(Long warehouseId, Long shopId){
        WmsWarehouseShop wmsWarehouseShop = new WmsWarehouseShop();
        wmsWarehouseShop.setWarehouseId(warehouseId);
        wmsWarehouseShop.setShopId(shopId);
        wmsWarehouseShopService.save(wmsWarehouseShop);
        return WrapMapper.ok();
    }

    @Override
    public List<WmsFlowApiDto> findOrderFlow(@RequestBody List<Long> collect) {
        WmsFlowDto wmsFlowDto = new WmsFlowDto();
        wmsFlowDto.setFlowOrderIdList(collect);
        List<WmsFlowDto> list = wmsFlowService.selectFlowIn(wmsFlowDto);
        if(list==null||list.size()==0){
            return null;
        }
        List<WmsFlowApiDto> wmsFlowApiDtos = new ArrayList<>();
        for (WmsFlowDto flowDto : list) {
            WmsFlowApiDto wmsFlowApiDto = new WmsFlowApiDto();
            BeanUtils.copyProperties(flowDto,wmsFlowApiDto);
            wmsFlowApiDtos.add(wmsFlowApiDto);
        }
        return wmsFlowApiDtos;
    }


}



